(function(f){
    var inRect=function(point,Obj){
        if(point.x>=Obj.x&&Obj.x+Obj.w>=point.x&&point.y>=Obj.y&&Obj.y+Obj.h>=point.y)return true;
        return false
    };
 
    function a(){
        var obj = new Object();
        var structObj = {
            processStartFun:function(ele,x,y,event){
                if(!ele.clone){
                    ele.clone=$("<div></div>");
                    ele.clone.addClass(obj.option.showClass)
                }
                obj.allChild=$("."+obj.option.arrClass);
                ele.beforeEle=0;
                ele.afterEle=null;
                if(obj.option.isShowCss){
                    ele.clone.css({
                        height:$(ele).outerHeight()+"px",
                        width:$(ele).outerWidth()+"px",
                        border:"1px solid #e6eaee",
                        backgroundColor:"#cccccc"
                    });
                }else{
                    ele.clone.attr("style","");
                }
                ele.index=obj.allChild.index(ele);
                obj.option.callBackStart.call(obj,{info:"dropStart",ele:ele,x:x,y:y,event:event});
            },
            processMoveFun:function(ele,x,y,event){
                if(!ele.clone){
                    ele.clone=$("<div></div>");
                    ele.clone.addClass(obj.option.showClass)
                }
                var first = null;
                var all = obj.allChild;
 
                all.each(function(){
                    if(first==null&&this!=ele){
                        first=$(this);
                    }
                    if(this==ele){return}
                    var p1={
                        x:x,
                        y:y
                    };
                    var rect={
                        x:$(this).offset().left,
                        y:$(this).offset().top,
                        w:$(this).outerWidth(),
                        h:$(this).outerHeight()
                    };
                    if(inRect(p1,rect)){
                        $(this).after(ele.clone);
                        ele.afterEle=this;
 
                    }
 
                    if(p1.x<first.offset().left+first.outerWidth()&&p1.x>first.offset().left&&p1.y<first.offset().top){
                        first.before(ele.clone);
                        ele.afterEle=first;
                        ele.beforeEle=1;
                    }
                });
                obj.option.callBackMove.call(obj,{info:"dropIng",ele:ele,x:x,y:y,event:event});
            },
            mode:"process",
            processEndFun:function(ele,x,y,event){
                if( ele.afterEle==null){
                    ele.style="";
                    ele.clone.remove();
                    obj.option.callBackEnd.call(obj,{info:"noChange",ele:ele,x:x,y:y,event:event});
                    return;
                }
                if(ele.beforeEle==1){
                    $(ele.afterEle).before(ele);
                }else{
                    $(ele.afterEle).after(ele);
                }
                ele.style="";
                $(ele).attr("style","");
                ele.clone.remove();
                var index=$("."+obj.option.arrClass).index(ele);
                if(index==ele.index){
                    obj.option.callBackEnd.call(obj,{info:"noChange",ele:ele,x:x,y:y,event:event});
                }else{
                    obj.option.callBackEnd.call(obj,{info:"change",ele:ele,x:x,y:y,event:event});
                }
            }
        };
        obj.setOption=function(Obj){
            var defaultObj = {
                maxBox:null,//最大容器
                arrClass:"info",//要拖动的节点的class
                showClass:"",//生成的节点的class
                callBackEnd:function(){},//回调
                callBackStart:function(){},
                callBackMove:function(){},
                isShowCss:true //是否给生成的节点增加样式
            };
            $.extend(defaultObj,Obj);
            obj.option = defaultObj;
 
        };
 
        obj.mouseDown=function(e){
            if(typeof this.typeDrop=="undefined"){
                structObj.processStartFun(this,0,0,e);
                $(this).dropIng(
                    1,structObj
                );
 
                var x= 0;
                var y= 0;
                x=$(this).offset().left-(window.scrollX?window.scrollX:window.pageXOffset);
                y=$(this).offset().top-(window.scrollY?window.scrollY:window.pageYOffset);
                this.startPoint={
                    x: e.clientX,
                    y: e.clientY,
                    Px:x,
                    Py:y
                };
                $(this).css({
                    position:"fixed",
                    cursor:"pointer",
                    left:parseInt(this.startPoint.Px)+"px",
                    top:parseInt(this.startPoint.Py)+"px",
                    margin:0
                });
            }
        };
        obj.init = function(){
            var maxBox = $(obj.option.maxBox);
 
            maxBox.on("mousedown","."+obj.option.arrClass,obj.mouseDown);
 
        };
        obj.remove=function(){
            var maxBox = $(obj.option.maxBox);
            maxBox.off("mousedown",obj.mouseDown);
        };
        return obj;
    }
    f(a);
})(function(f){
    $.fn.dropProtoTitle=function(Obj){
        if(!$(document).dropIng){
            console.log("error:dropIng is not defined");
            return {};
        }
        var obj = f();
        Obj=Obj||{};
        if(!Obj.maxBox){
            Obj.maxBox = this;
        }
        obj.setOption(Obj);
        obj.init();
        return obj;
    }
 
});